package saucaca.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import saucaca.Adapter.ArtistAdapter;
import saucaca.Adapter.NewsAdapter;
import saucaca.CustomView.ProgressBarCircularIndeterminate;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.NewsObject;
import saucaca.Object.SingerObject;
import saucaca.fragment.NewFragment.GetFromServices;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;

public class ArtistList extends Fragment implements OnScrollListener {
	public static final String TAG = ArtistList.class.getSimpleName();
	List<SingerObject> listSinger;
	String sType = "2";
	String areaCode = "";
	String page = "0";
	String pageSize = "";
	int xpage = 0;

	MainActivity mActivity;
	private ListView listView;
	boolean loadingMore;
	private View mView_EmptyLoading = null;
	private ProgressBar progressBar;
	ImageView imgEmpty, imgError;
	TextView txtStatus;
	boolean isFromMenu;
	TextView txtTitle;
	ArtistAdapter adapter;
	Context context;
	// private SwipeRefreshLayout mSwipeLayout;

	ProgressBarCircularIndeterminate progressBarFooter;
	int m_PreviousTotalCount = 0;

	public static ArtistList newInstance(String SongId) {
		ArtistList f = new ArtistList();
		Bundle args = new Bundle();
		args.putString(MainActivity.ID, SongId);
		f.setArguments(args);
		return f;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mActivity = (MainActivity) getActivity();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View android = inflater.inflate(R.layout.listview2, container, false);
		context = getActivity();
		findviewByID(android);
		if (getArguments() != null) {
			Bundle b = getArguments();
			areaCode = b.getString(MainActivity.ID);
		}
		new GetFromServices().execute("");
		listSinger = new ArrayList<SingerObject>();
		adapter = new ArtistAdapter(context, listSinger);
		listView.setAdapter(adapter);
		return android;
	}

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	private void findviewByID(View view) {
		listView = (ListView) view.findViewById(R.id.layoutlistview_listview);
		mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		listView.setEmptyView(mView_EmptyLoading);
		listView.setOnScrollListener(this);
		progressBarFooter = (ProgressBarCircularIndeterminate) view.findViewById(R.id.progressBarloadmore);
		progressBar = (ProgressBar) mView_EmptyLoading.findViewById(R.id.empty_loading_progress);
		imgEmpty = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_NoItem);
		imgError = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_Error);
		txtStatus = (TextView) mView_EmptyLoading.findViewById(R.id.empty_loading_text);

	}

	class GetFromServices extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... param) {
			if (m_PreviousTotalCount > 0)
				getActivity().runOnUiThread(new Runnable() {
					@Override
					public void run() {
						progressBarFooter.setVisibility(View.VISIBLE);
					}
				});
			page = Integer.toString(xpage);
			loadingMore = true;
			String str = WServiceLib.artist(page, pageSize, areaCode);
			// BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null)
				try {
					if (getActivity()!=null)
					getActivity().runOnUiThread(new Runnable() {
						@Override
						public void run() {
							progressBarFooter.setVisibility(View.GONE);
						}
					});
					if (listSinger != null) {
						List<SingerObject> singerObjects = JsonReader.getArtistReader(result);
						if (singerObjects != null && singerObjects.size() > 0) {
							for (int i = 0; i < singerObjects.size(); i++) {
								boolean isAdded = false;
								for (int j = 0; j < listSinger.size(); j++) {
									if (listSinger.get(j).getSINGER_ID().equals(singerObjects.get(i).getSINGER_ID())) {
										isAdded = true;
									}
								}
								if (!isAdded) {
									listSinger.add(singerObjects.get(i));
									adapter.update(listSinger);
								}
							}
						}
						loadingMore = false;
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (totalItemCount == 0 || adapter == null)
			return;
		if (m_PreviousTotalCount == totalItemCount) {
			return;
		}
		boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount - 3;
		if (loadMore && firstVisibleItem > 0 && totalItemCount - m_PreviousTotalCount >= 10) {
			m_PreviousTotalCount = totalItemCount;
			xpage += 1;
			new GetFromServices().execute("");
		}

	}
}
